package com.hxm.webview.view;

import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.hxm.webview.R;
import com.hxm.webview.entity.NewsDetail;
import com.hxm.webview.http.HttpRequest;
import com.hxm.webview.http.listener.RequestListener;

/**
 * Created by HXM on 2016/8/8.
 * Description:
 */
public class NewsDetailActivity extends AppCompatActivity {
    private WebView webView;
    private String newsContent;
    private String newsId;
    private NewsDetailListener listener;
    private LinearLayout layoutContainer;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_news_detail);
        newsId=getIntent().getStringExtra("newsId");
        initView();
        getNewsDetail();
    }
    @SuppressWarnings("setJavaScriptEnabled")
    private void initView() {
        listener=new NewsDetailListener();
        layoutContainer= (LinearLayout) findViewById(R.id.container);
        progressBar= (ProgressBar) findViewById(R.id.progressBar);
//        webView=new WebView(getApplicationContext());
//        layoutContainer.addView(webView);
        webView= (WebView) findViewById(R.id.webview);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                progressBar.setProgress(newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Toast.makeText(getApplicationContext(),title,Toast.LENGTH_SHORT).show();
            }
        });
        WebSettings settings=webView.getSettings();
        settings.setJavaScriptEnabled(true);//设置允许运行JavaScript
        settings.setAppCacheEnabled(false);//设置允许缓存
        settings.setDatabaseEnabled(true);//设置允许使用localstore
        settings.setDefaultFontSize(16);
        settings.setBlockNetworkImage(true);
        settings.setBlockNetworkImage(false);
        settings.setBlockNetworkLoads(false);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//        settings.setAppCachePath(this.getCacheDir().getPath());
       // settings.setLoadsImagesAutomatically(true);//支持自动加载图片
    }
    //按下返回键返回上一个加载的界面
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

    private void getNewsDetail(){
        HttpRequest request=new HttpRequest("http://m.mydrivers.com");
        request.getNewsDetail(newsId,listener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        layoutContainer.removeAllViews();
        webView.stopLoading();
        webView.removeAllViews();
        webView.destroy();
        Log.i("destory","lalalala");
        webView.clearCache(true);
        webView=null;

    }

    public class NewsDetailListener implements RequestListener<NewsDetail>{

        @Override
        public void success(NewsDetail data) {
            Log.i("newsDeatil",data.getContent());
            webView.loadDataWithBaseURL(null,data.getContent(),"text/html;charset=UTF-8",null,null);
        }

        @Override
        public void failed(String error) {
            Log.i("newsDeatil",error);
            webView.loadUrl("http:\\/\\/v.juhe.cn\\/weixin\\/redirect?wid=wechat_20160811031971");
        }
    }
}
